load('application');

before(loadGroupUser, {only: ['show', 'edit', 'update', 'destroy']});

action('new', function () {
    this.title = 'New groupuser';
    this.groupuser = new GroupUser;
    render();
});

action(function create() {
    GroupUser.create(req.body.GroupUser, function (err, groupuser) {
        if (err) {
            flash('error', 'GroupUser can not be created');
            render('new', {
                groupuser: groupuser,
                title: 'New groupuser'
            });
        } else {
            flash('info', 'GroupUser created');
            redirect(path_to.groupusers());
        }
    });
});

action(function index() {
    this.title = 'GroupUsers index';
    GroupUser.all(function (err, groupusers) {
        render({
            groupusers: groupusers
        });
    });
});

action(function show() {
    this.title = 'GroupUser show';
    render();
});

action(function edit() {
    this.title = 'GroupUser edit';
    render();
});

action(function update() {
    this.groupuser.updateAttributes(body.GroupUser, function (err) {
        if (!err) {
            flash('info', 'GroupUser updated');
            redirect(path_to.GroupUser(this.GroupUser));
        } else {
            flash('error', 'GroupUser can not be updated');
            this.title = 'Edit groupuser details';
            render('edit');
        }
    }.bind(this));
});

action(function destroy() {
    this.groupuser.destroy(function (error) {
        if (error) {
            flash('error', 'Can not destroy groupuser');
        } else {
            flash('info', 'GroupUser successfully removed');
        }
        send("'" + path_to.groupusers() + "'");
    });
});

function loadGroupUser() {
    GroupUser.find(params.id, function (err, groupuser) {
        if (err || !groupuser) {
            redirect(path_to.groupusers());
        } else {
            this.groupuser = groupuser;
            next();
        }
    }.bind(this));
}
